<?php
/**
 * User: hey
 * Date: 2019/1/9
 * Time: 16:56
 */

namespace app;



use epii\server\Args;
use service\admin\AdminService;
use service\admin\EmployeePerformanceService;
use service\admin\RoleService;
use service\admin\ScheduleService;
use service\admin\TreatmentService;
use service\BaseService;
use think\Db;

class employee extends base
{


    public function index()
    {
        $this->assign('roles', RoleService::getSelect('id>=3'));
        $this->adminUiDisplay('employee/index', "", ["version" => time()]);
    }

    /**
     * 表格数据
     */
    public function ajaxdata()
    {
        $map = [];

        //雇员列表仅展示院管理员以下的角色
        $map[] = ['a.role','>=',RoleService::$role['doctor']['id']];

        if(($hospital_id = BaseService::getAdminInfo()['hospital_id'])>0){
            $map[] = ['a.hospital_id','=',$hospital_id];
        }

        $group_name = Args::postVal('group_name');
        if ($group_name) {
            $map[] = ['a.group_name','like',"%{$group_name}%"];
        }

        $phone = Args::postVal('username');

        if ($phone) {
            $map[] = ['a.username','=',$phone];
        }

        $role = Args::postVal('role');
        if ($role) {
            $map[] = ['a.role','=',$role];
        }
        echo AdminService::getAjaxData($map);

    }


    public function add()
    {

        if (Args::isPost()) {

            $id = Args::params("id/d");

            $username = trim(Args::params("username/1"));
            $password = Args::params("password" . ($id ? "" : "/1"));
            $group_name = trim(Args::params("group_name/1"));
            $status = trim(Args::params("status/1"));
            $role = trim(Args::params("role/1"));
            $school = trim(Args::params("school"));
            $brief = trim(Args::params("brief"));//简介


            if (!preg_match("/^1\d{10}$/", $username)) {
               $this->error('手机号格式错误');
            }

            if ($password)
                if (!preg_match("/^[a-zA-Z\d_]{6,16}$/", $password)) {
                   $this->error('密码为6~16位，可包含的字符为数字和大小写字母');
                }


            if (!preg_match("/^[\x{4e00}-\x{9fa5}]{2,8}$/u", $group_name)) {
               $this->error('姓名为2~8个汉字');
            }


            $data = [];
            $data['hospital_id'] = BaseService::getAdminInfo()['hospital_id'];

            $data['username'] = $username;
            $data['phone'] = $username;
            if ($password)
                $data['password'] = md5($password);
            $data['group_name'] = $group_name;
            $data['status'] = $status;
            $data['role'] = $role;
            $data['school'] = $school;
            $data['brief'] = $brief;

            $data['updatetime'] = time();

            if (!$id) {
                $has = AdminService::getInfo(['username'=>$username,'hospital_id'=>BaseService::getAdminInfo()['hospital_id']]);
                if ($has) {
                    $this->error('该手机号已经存在');
                }
                $data['addtime'] = time();
                $res = AdminService::add($data);
            } else {
                $res = AdminService::edit($id,$data);
            }

            if ($res) {
                $this->success();
            } else {
               $this->error();
            }

        } else {

            // 只能医院管理员添加
            if(!BaseService::getAdminInfo()['hospital_id']){
                echo "<script>alert('请联系诊所管理员添加雇员');</script>";
                exit;
            }

            if ($id = Args::params("id/d")) {
                $this->_as_admin = AdminService::getInfoById($id);
            }
            $this->assign('roles', RoleService::getSelect('id>=3'));
            $this->assign("status_arr", [["value" => "normal", "text" => "正常"], ["value" => "locked", "text" => "锁定"]]);
            $this->adminUiDisplay('employee/add');
        }
    }

    public function jixiao(){
        $admin_id = Args::params('admin_id');
        if(Args::isPost()){
            $ids = Args::params('id');
            $moneys = Args::params('money');
            $hospitalId = $this->adminInfo['hospital_id'];
            EmployeePerformanceService::addEmployeePerformance($hospitalId,$admin_id,$ids,$moneys);
            $this->success();
        }else{
            $this->assign('admin_id',$admin_id);
            $this->assign('list',EmployeePerformanceService::getEmployeePerformance($admin_id));
            $this->adminUiDisplay();
        }
    }
    //问诊排班
    public function schedule()
    {
        $admin_id=Args::params('admin_id/d');
        $data=ScheduleService::getAjaxData($this->adminInfo['hospital_id'],$admin_id);
        $this->assign('data',$data);
        $this->adminUiDisplay();

    }


}